package cn.wh.user.service.impl;

import cn.wh.basic.constants.BaseConstant;
import cn.wh.basic.dto.RegisterDto;
import cn.wh.basic.exception.BusinessException;
import cn.wh.basic.util.MD5Utils;
import cn.wh.basic.util.StrUtils;
import cn.wh.org.domain.Employee;
import cn.wh.user.domain.Logininfo;
import cn.wh.user.domain.User;
import cn.wh.user.mapper.LogininfoMapper;
import cn.wh.user.mapper.UserMapper;
import cn.wh.user.service.IUserService;
import cn.wh.basic.service.impl.BaseServiceImpl;
import com.baomidou.mybatisplus.core.injector.methods.DeleteById;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类  注册
 * </p>
 *
 * @author yaosang
 * @since 2022-06-16
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public void phoneRegister(RegisterDto dto) {
        String phone = dto.getPhone();
        String password = dto.getPassword();
        String passwordRepeat = dto.getPasswordRepeat();
        String smsCode = dto.getSmsCode();
        //1.校验
        // 空校验
        if (StringUtils.isEmpty(phone)||
                StringUtils.isEmpty(password)||
                StringUtils.isEmpty(passwordRepeat)||
                StringUtils.isEmpty(smsCode)){
            throw new BusinessException("你这样子填,我能给你注册?");
        }
        // 密码和确认密码是否一致
        if (!password.equals(passwordRepeat)){
            throw new BusinessException("两次密码不一致呀 ,你在干嘛呀");
        }
        //2.短信验证码判断
        String key2 = "re:" + phone;
        Object smsCodeTmp = redisTemplate.opsForValue().get(key2);
        //2.1 拿不到  验证码过期，请重新获取
        if (smsCodeTmp == null){
            throw new BusinessException("验证码过期了,您的操作能不能快一点,给我重新来");
        }
        //2.2 拿到了，判断是否一致
        if (!smsCode.equalsIgnoreCase(smsCodeTmp.toString().split(":")[0])){
            //  。 不一致 验证码错误，请核对
            throw new BusinessException("验证码都输错了,你在搞笑吗");
        }
        System.out.println(smsCode);
        //  。一致，往下走
        //3.保存数据 t_user + t_logininfo
        User user = dto2User(dto);
        Logininfo logininfo = user2Logininfo(user);
        logininfoMapper.save(logininfo);
        user.setLogininfoId(logininfo.getId());
        userMapper.save(user);
    }

    @Override
    public User loadByLoginInfo(Long id) {
        return userMapper.loadByLoginIn(id);
    }

    private Logininfo user2Logininfo(User user) {
        //第一步新建一个对象
        Logininfo logininfo = new Logininfo();
        //第二步操作
        BeanUtils.copyProperties(user,logininfo);
        //设置用户权限
        logininfo.setType(1);
        //第三步返回
        return logininfo;
    }

    private User dto2User(RegisterDto dto) {
        //第一步新建一个对象
        User user = new User();
        //第二步操作
        user.setUsername(dto.getPhone());
        user.setPhone(dto.getPhone());
        String salt = StrUtils.getComplexRandomString(32);
        String byMd5 = MD5Utils.encrypByMd5(dto.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(byMd5);
        //第三步返回
        return user;
    }

    @Override
    public void update(User user) {
        userMapper.update(user);
        Logininfo logininfo = logininfoMapper.loadById(user.getLogininfoId());
        BeanUtils.copyProperties(user,logininfo);
        logininfoMapper.update(logininfo);
    }

    @Override
    public void removeById(Long id) {
        User user = userMapper.loadById(id);
        userMapper.deleteById(id);
        logininfoMapper.deleteById(user.getLogininfoId());
    }
}
